using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;

namespace CodeFx
{
    public static class FileHelper
    {
        public static string GetDirFromFile(string strDir)
        {
            if (string.IsNullOrEmpty(strDir))
                return string.Empty;
            int index = strDir.LastIndexOf("\\");
            if (index > 1)
                return strDir.Substring(0, index);
            else
                return string.Empty;
        }

        public static string GetFile(string fichier)
        {
            if (!string.IsNullOrEmpty(fichier))
            {
                int _LastIndexOf = 0;
                if (fichier.LastIndexOf("\\") > 0)
                    _LastIndexOf = fichier.LastIndexOf("\\");
                if ((_LastIndexOf == 0) && (fichier.LastIndexOf("/") > 0))
                    _LastIndexOf = fichier.LastIndexOf("/");
                if (_LastIndexOf > 0)
                    return fichier.Substring(_LastIndexOf + 1);
                else
                    return fichier;
            }
            else
                return (string)null;
        }


        public static void Resize(ref int imageWidth, ref int imageHeight, int maxWidth, int maxHeight)
        {
            double _imageWidth = Convert.ToDouble(imageWidth);
            double _imageHeight = Convert.ToDouble(imageHeight);
            double _maxWidth = Convert.ToDouble(maxWidth);
            double _maxHeight = Convert.ToDouble(maxHeight);
            if (_imageWidth > _maxWidth)
            {
                _imageHeight = System.Math.Ceiling(_imageHeight * (_maxWidth / _imageWidth));
                _imageWidth = _maxWidth;
            }
            if (_imageHeight > _maxHeight)
            {
                _imageWidth = System.Math.Ceiling(_imageWidth * (_maxHeight / _imageHeight));
                _imageHeight = _maxHeight;
            }
            imageWidth = Convert.ToInt32(_imageWidth);
            imageHeight = Convert.ToInt32(_imageHeight);
        }

        public static string CreateThumb(string dircible, string fileprev, string nomlogo, int finaloutwidth, int finaloutheight)
        {
            StringBuilder _st = new StringBuilder();
            _st.Append(dircible);
            _st.Append(nomlogo);
            string t_thumbfn = _st.ToString().ToLower(CultureInfo.InvariantCulture);
            if (File.Exists(t_thumbfn))
                File.Delete(t_thumbfn);
            System.Drawing.Bitmap graphic = new System.Drawing.Bitmap(fileprev);

            int thumbWidth = graphic.Width;
            int thumbHeight = graphic.Height;

            Resize(ref thumbWidth, ref thumbHeight, finaloutwidth, finaloutheight);
            System.Drawing.Image imgThumbnail = graphic.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
            t_thumbfn = t_thumbfn.Replace("\\", "/");
            imgThumbnail.Save(t_thumbfn, graphic.RawFormat);
            graphic.Dispose();

            return t_thumbfn;
        }

        public static void DirSize(DirectoryInfo d, ref long nbrfiles, ref long nbrdir, ref float ko)
        {
            // Add file sizes.
            if (d.Exists)
            {
                nbrdir += 1;
                FileInfo[] fis = d.GetFiles();
                foreach (FileInfo fi in fis)
                {
                    ko += (fi.Length / 1024);
                    nbrfiles += 1;
                }
                // Add subdirectory sizes.
                DirectoryInfo[] dis = d.GetDirectories();
                foreach (DirectoryInfo di in dis)
                {
                    nbrdir += 1;
                    DirSize(di, ref nbrfiles, ref nbrdir, ref ko);
                }
            }
        }

        public static void DeleteDirectory(string rep)
        {
            if (string.IsNullOrEmpty(rep))
                return;
            DirectoryInfo dir = new DirectoryInfo(rep);
            if (dir.Exists)
                dir.Delete(true);
       }

        public static void DeleteFile(string file)
        {
            if (string.IsNullOrEmpty(file))
                return;
            if (File.Exists(file))
                File.Delete(file);
        }

    }
}
